package com.atguigu.udmp.web.userprofile.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.udmp.web.userprofile.bean.TagInfo;
import com.atguigu.udmp.web.userprofile.service.TagInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-10
 */
@RestController
@RequestMapping("/userprofile/tag")
@CrossOrigin
public class TagInfoController {



        @Autowired
        TagInfoService tagInfoService;

        @RequestMapping("/list")
        public String tagInfoList(){
            //1 查询整个标签集合列表
            List<TagInfo> tagInfoList = tagInfoService.getTagInfoAllWithStatus();


            //2 获得父标签下所有子标签的Map集合
            Map<Long ,List<TagInfo>> tagTreeNodeMap = new HashMap();
            for (TagInfo tagInfo : tagInfoList) {
                List<TagInfo> childTagInfoList = tagTreeNodeMap.get(tagInfo.getParentTagId());
                if(childTagInfoList==null){
                    childTagInfoList= new ArrayList<>();
                    tagTreeNodeMap.put(tagInfo.getParentTagId(),childTagInfoList);
                }
                childTagInfoList.add(tagInfo);
            }

            //3 从map找对各个节点子标签，进行装配
            for (TagInfo tagInfo : tagInfoList) {
                List<TagInfo> childTagInfoList = tagTreeNodeMap.get(tagInfo.getId());
                tagInfo.setChildren(childTagInfoList);
            }
            //4 只保留根节点
            List<TagInfo> tagInfo1LevelList = tagInfoList.stream().filter(tagInfo -> tagInfo.getTagLevel() == 1).collect(Collectors.toList());

            return JSON.toJSONString(tagInfo1LevelList );

        }

    @PostMapping("/detail")
    public String saveTagInfo(@RequestBody TagInfo tagInfo){
        if(tagInfo.getId()==null){
            tagInfo.setCreateTime(new Date());
        }else{
            tagInfo.setUpdateTime(new Date());
        }
        tagInfoService.saveOrUpdate(tagInfo);
        return "success";
    }

    @GetMapping("/detail/{tagId}")
    public String getTagInfo(@PathVariable("tagId") Long tagId){
        TagInfo tagInfo =  tagInfoService.getById(tagId);
        if(tagInfo.getTagLevel()>1L){
            TagInfo parentTagInfo = tagInfoService.getById(tagInfo.getParentTagId());
            tagInfo.setParentTagLevel(parentTagInfo.getTagLevel());
            tagInfo.setParentTagName(parentTagInfo.getTagName());
            tagInfo.setParentTagCode(parentTagInfo.getTagCode());
        }
        return  JSON.toJSONString(tagInfo) ;
    }

    @DeleteMapping("/detail/{tagId}")
    public String deleteTagInfo(@PathVariable("tagId") Long tagId){
        int subTagCount = tagInfoService.count(new QueryWrapper<TagInfo>().eq("parent_tag_id", tagId));
        if(subTagCount>0){
            return "存在子标签";
        }
        tagInfoService.removeById(tagId);
        return  "success" ;
    }

    @GetMapping("/subtags/{parentTagId}")
    public String tagInfoList(@PathVariable("parentTagId") String parentTagId){

        List<TagInfo> subTagInfoList = tagInfoService.list(new QueryWrapper<TagInfo>().eq("parent_tag_id", Long.valueOf(parentTagId)));
        List<String> subTagValue = subTagInfoList.stream().map(tagInfo -> tagInfo.getTagName()).collect(Collectors.toList());
        String tagJson = JSON.toJSONString(subTagValue);

        return tagJson;
    }

}
